package com.brillilab.service.logic.method;

import com.brillilab.common.alioss.OssFileEnum;
import com.brillilab.common.constant.ResultEnum;
import com.brillilab.domain.enums.lab.LabMemberPowerEnum;
import com.brillilab.domain.enums.method.MethodDealStateEnum;
import com.brillilab.domain.enums.method.MethodSourceEnum;
import com.brillilab.domain.enums.method.MethodTypeLevelEnum;
import com.brillilab.domain.enums.method.RecommendAuditStateEnum;
import com.brillilab.domain.po.lab.LabMember;
import com.brillilab.domain.po.lab.LabMemberPower;
import com.brillilab.domain.po.method.*;
import com.brillilab.domain.po.system.FileManage;
import com.brillilab.domain.po.user.Users;
import com.brillilab.domain.vo.home.HomeMyAuditVo;
import com.brillilab.domain.vo.lab.LabMemberVo;
import com.brillilab.domain.vo.method.*;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.lab.ILabMemberPowerService;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.core.lab.ILabService;
import com.brillilab.service.core.method.IMethodRecommendAuditService;
import com.brillilab.service.core.method.IMethodRecommendService;
import com.brillilab.service.core.method.IMethodService;
import com.brillilab.service.core.method.IMethodTypeService;
import com.brillilab.service.core.system.IFileManageService;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.service.module.message.producer.MessageProducer;
import com.brillilab.service.module.message.template.enums.MessageTypeEnum;
import com.brillilab.service.module.message.template.impl.method.MethodToShareOtherTemp;
import com.brillilab.service.module.message.template.impl.method.RecommendMethodAuditPassTemp;
import com.brillilab.service.module.message.template.impl.method.RecommendMethodModifyAuditPassTemp;
import com.brillilab.service.module.message.template.impl.method.SingleRecommendMethodTemp;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class MethodRecommendAuditLogic {

    @Resource
    private MethodRecommendLogic methodRecommendLogic;
    @Resource
    private IMethodRecommendService methodRecommendService;
    @Resource
    private ILabMemberService labMemberService;
    @Resource
    private ILabService labService;
    @Resource
    private IMethodRecommendAuditService methodRecommendAuditService;
    @Resource
    private IMethodService methodService;
    @Resource
    private IMethodTypeService methodTypeService;
    @Resource
    private MethodLogic methodLogic;
    @Resource
    private MessageProducer messageProducer;
    @Resource
    private IFileManageService fileManageService;
    @Resource
    private ILabMemberPowerService labMemberPowerService;
    @Resource
    private UsersLogic usersLogic;

    /**
     * 审核通过推荐方法
     *
     * @param userVo
     * @param auditId
     * @param isAdmin 是否为管理员审核
     */
    public MethodRecommendAudit auditAdopt(UserInfoVo userVo, Long auditId, boolean isAdmin) {
        Assert.isTrue(userVo != null && auditId != null, "参数缺失");
        MethodRecommendAudit audit = methodRecommendAuditService.selectById(auditId);
        Assert.isTrue(audit.getLabId().equals(userVo.getLabId()), "非该审核记录实验室登录，无权限审核");
        Assert.isTrue(audit.getState().equals(RecommendAuditStateEnum.AuditWait.getState()), "该方法已审核过");
        // 设为推荐方法
        Method recommendMethod = methodService.getById(audit.getMethodId());
        Long recommendMethodId = recommendMethod.getId();
        recommendMethod.setRecommendMethodId(recommendMethodId);
        methodService.updateById(recommendMethod);

        //标记审核通过
        audit.setState(RecommendAuditStateEnum.Recommend.getState());
        // 该方法不是审核人员的，给审核人员拷贝一份
        if (!userVo.getId().equals(recommendMethod.getUserId())) {
            Long methodId = methodRecommendLogic.dealToMyNewMethod(userVo, recommendMethod, MethodSourceEnum.RECOMMEND,
                    null, new Date(), userVo.getLabName(), null);
            Method method = new Method();
            method.setId(methodId);
            method.setRecommendMethodId(recommendMethodId);
            methodService.updateById(method);

            //fixme 审核通过，执行附件处理
            if (audit.getState() == 1 && recommendMethodId != null) {
                List<FileManage> fileList = fileManageService.selectList(OssFileEnum.Method, recommendMethodId);
                if (!CollectionUtils.isEmpty(fileList)) {
                    List<FileManage> copies = new ArrayList<>();
                    for (FileManage fileManage : fileList) {
                        fileManage.setId(null);
                        fileManage.setReferDataId(methodId);
                        fileManage.setUserId(userVo.getId());
                        fileManage.setLabId(userVo.getLabId());
                        fileManage.setCreateTime(null);
                        copies.add(fileManage);
                    }
                    fileManageService.insertBatch(copies);
                }
            }
        }

        // 推荐给其他实验室成员
        MethodRecommend recommend = new MethodRecommend();
        recommend.setMethodId(recommendMethodId);
        recommend.setLabId(userVo.getLabId());
        recommend.setRecommendTime(new Date());
        recommend = methodRecommendService.addRecommend(recommend);

        // 审核人员默认接收一份
        MethodRecommendReceive receive = new MethodRecommendReceive();
        receive.setLabId(userVo.getLabId());
        receive.setLabMemberId(userVo.getLabMemberId());
        receive.setMethodId(recommend.getMethodId());
        receive.setState(MethodDealStateEnum.Accept.getState());
        receive.setMethodRecommendId(recommend.getId());
        methodRecommendService.saveReceive(receive);

        // 推荐人默认接收
        if (!audit.getRecommenderId().equals(userVo.getLabMemberId())) {
            receive.setId(null);
            receive.setLabMemberId(audit.getRecommenderId());
            methodRecommendService.saveReceive(receive);
        }

        // 更新审核数据
        audit.setAuditorId(userVo.getLabMemberId());
        audit.setAuditTime(new Date());
        audit.setRecommendMethodId(recommendMethodId);
        int i = methodRecommendAuditService.updateById(audit);
        Assert.isTrue(i == 1, "审核通过失败");

        if (!isAdmin && audit.getState() == 1) {

            //管理员审核通过，执行消息推送(发送给方法推荐人)
            RecommendMethodAuditPassTemp recommendMethodAuditPassTemp = new RecommendMethodAuditPassTemp(audit.getLabId(), recommendMethod.getUserId(), audit.getRecommenderId(), recommendMethod.getName());
            messageProducer.sendMessagePush(recommendMethodAuditPassTemp);

            //推送给实验室所有成员
            sendMsgLabMemberList(userVo, audit, recommendMethod);
        }

        return audit;
    }


    /**
     * 方法审核通过，推送给实验室所有成员
     *
     * @param userVo
     * @param audit
     * @param recommendMethod
     */
    public void sendMsgLabMemberList(UserInfoVo userVo, MethodRecommendAudit audit, Method recommendMethod) {
        Assert.notNull(recommendMethod, ResultEnum.DATA_NOT_EXIST.getMessage());
        //推送给实验室所有成员(过滤操作人本人、推荐人、方法所有者)
        List<LabMemberVo> labMemberVos = labMemberService.selectList(userVo.getLabId(), 1).stream().filter(e -> !e.getUserId().equals(userVo.getId()) && !e.getLabMemberId().equals(audit.getRecommenderId()) && !e.getUserId().equals(recommendMethod.getUserId())).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(labMemberVos)) {
            //推荐方法信息
            MethodRecommend methodRecommend = methodRecommendService.getRecommendByLabIdAndMethodId(audit.getLabId(), audit.getRecommendMethodId());
            Assert.notNull(methodRecommend, ResultEnum.DATA_NOT_EXIST.getMessage());
            //推荐人成员信息
            LabMember recommendLabMember = labMemberService.selectById(audit.getRecommenderId());
            Map<Long, Long> idMap=labMemberVos.stream().collect(Collectors.toMap(LabMemberVo::getUserId,LabMemberVo::getLabMemberId));

            SingleRecommendMethodTemp temp=new SingleRecommendMethodTemp(userVo.getLabId(),idMap,userVo.getName(),recommendMethod.getName(),methodRecommend.getId(),audit.getId());
            messageProducer.sendMessagePushMutiReceiver(temp);
        }
    }

    /**
     * 管理员推荐方法直接通过
     *
     * @param userVo
     * @param methodIds
     */
    public void adminRecommend(UserInfoVo userVo, List<Long> methodIds) {

        Assert.isTrue(userVo != null && !CollectionUtils.isEmpty(methodIds), "参数缺失");
        List<MethodRecommendAudit> list = methodRecommendAuditService.selectByMethodIds(userVo.getLabId(), methodIds);
        Assert.isTrue(CollectionUtils.isEmpty(list), "方法不能重复推荐");

        List<MethodRecommendAudit> auditList = new ArrayList<>();
        for (Long methodId : methodIds) {
            Method method = methodService.getById(methodId);
            Assert.notNull(method, "推荐方法不存在");
            MethodType methodType = methodTypeService.getMethodTypeById(method.getMethodTypeId());
            MethodRecommendAudit audit = new MethodRecommendAudit();
            audit.setLabId(userVo.getLabId());
            audit.setFirstMethodTypeId(methodType.getFirstId());
            audit.setSecondMethodTypeId(methodType.getSecondId());
            audit.setMethodTypeId(methodType.getId());
            audit.setMethodId(method.getId());
            audit.setState(RecommendAuditStateEnum.AuditWait.getState());
            audit.setRecommenderId(userVo.getLabMemberId());
            boolean add = methodRecommendAuditService.add(audit);
            Assert.isTrue(add, "推荐方法失败");
            auditList.add(this.auditAdopt(userVo,audit.getId(),true));
        }

        //推荐成功，执行消息推送
        new Thread(() ->adminRecommendPush(auditList, userVo)).start();
    }


    //推荐成功，执行消息推送
    private void adminRecommendPush(List<MethodRecommendAudit> auditList, UserInfoVo userVo) {
        //推送给实验室所有成员(过滤推荐人本人)
        List<LabMemberVo> labMemberVos = labMemberService.selectList(userVo.getLabId(), 1).stream().filter(m->!m.getUserId().equals(userVo.getId())).collect(Collectors.toList());
        if (labMemberVos != null && labMemberVos.size() > 0) {
            Map<Long, Long> idMap=labMemberVos.stream().collect(Collectors.toMap(LabMemberVo::getUserId,LabMemberVo::getLabMemberId));
            if (auditList != null && auditList.size() > 0) {

                auditList.forEach(audit -> {
                    //方法信息
                    Method recommendMethod = methodService.getById(audit.getMethodId());
                    Assert.notNull(recommendMethod, ResultEnum.DATA_NOT_EXIST.getMessage());
                    //推荐方法信息
                    MethodRecommend methodRecommend = methodRecommendService.getRecommendByLabIdAndMethodId(audit.getLabId(), audit.getRecommendMethodId());
                    Assert.notNull(methodRecommend, ResultEnum.DATA_NOT_EXIST.getMessage());
                    //推荐人成员信息
                    LabMember recommendLabMember = labMemberService.selectById(audit.getRecommenderId());

                    //过滤方法所有者
                    if(recommendMethod.getUserId()!=null) {
                        if(idMap.get(recommendMethod.getUserId())!=null) {
                            idMap.remove(recommendMethod.getUserId());
                        }
                    }
                    SingleRecommendMethodTemp temp = new SingleRecommendMethodTemp(userVo.getLabId(), idMap, userVo.getName(), recommendMethod.getName(), methodRecommend.getId(), audit.getId());
                    messageProducer.sendMessagePushMutiReceiver(temp);
                });
            }
        }

        //推送给相关方法成员
        if (auditList != null && auditList.size() > 0) {
            auditList.forEach(audit -> {
                //根据方法ID获取方法信息
                Method recommendMethod = methodService.getById(audit.getMethodId());
                if(recommendMethod!=null){
                    //不推送给自己，方法所有者非操作人本人，执行推送通知
                    if(recommendMethod.getUserId()!=null && !recommendMethod.getUserId().equals(userVo.getId())){
                        LabMember recommendLabMember = labMemberService.selectMember(userVo.getLabId(), userVo.getId());
                        MethodToShareOtherTemp methodToShareOtherTemp = new MethodToShareOtherTemp(userVo.getLabId(), recommendMethod.getUserId(), recommendLabMember.getId(), recommendMethod.getName(), userVo.getName(),audit.getMethodId());
                        messageProducer.sendMessagePush(methodToShareOtherTemp);
                    }
                }
            });
        }

    }

    /**
     * 管理员推荐方法编辑通过
     *
     * @param userVo
     * @param newMethod
     */
    public void adminEditRecommend(UserInfoVo userVo, NewMethod newMethod) {
        Assert.isTrue(userVo != null && newMethod.getAuditId() != null, "参数缺失");
        MethodRecommendAudit audit = methodRecommendAuditService.selectById(newMethod.getAuditId());
        Assert.isTrue(audit.getState().equals(RecommendAuditStateEnum.AuditWait.getState()), "该方法已审核过");
        newMethod.setFromType(MethodSourceEnum.RECOMMEND.getSourceType());
        newMethod.setFromTime(audit.getCreateTime());
        Long recommendMethodId = methodRecommendLogic.auditorSaveToRecommendMethod(newMethod, userVo);
        Assert.notNull(recommendMethodId, "审核通过失败");

        //更新审核数据
        audit.setState(RecommendAuditStateEnum.Recommend.getState());
        audit.setAuditorId(userVo.getLabMemberId());
        audit.setAuditTime(new Date());
        audit.setRecommendMethodId(recommendMethodId);
        int i = methodRecommendAuditService.updateById(audit);
        Assert.isTrue(i == 1, "审核通过失败");

        // 推荐给其他实验室成员
        MethodRecommend recommend = new MethodRecommend();
        recommend.setMethodId(recommendMethodId);
        recommend.setLabId(userVo.getLabId());
        recommend.setRecommendTime(new Date());
        recommend = methodRecommendService.addRecommend(recommend);

        // 审核人员默认接收一份
        MethodRecommendReceive auditReceive = new MethodRecommendReceive();
        auditReceive.setLabId(userVo.getLabId());
        auditReceive.setLabMemberId(userVo.getLabMemberId());
        auditReceive.setMethodId(recommend.getMethodId());
        auditReceive.setState(MethodDealStateEnum.Accept.getState());
        auditReceive.setMethodRecommendId(recommend.getId());
        methodRecommendService.saveReceive(auditReceive);

        // 推荐人默认接收一份
        if (!audit.getRecommenderId().equals(audit.getAuditorId())) {
            MethodRecommendDeal recommendDeal = new MethodRecommendDeal();
            recommendDeal.setRecommendId(recommend.getId());
            recommendDeal.setState(MethodDealStateEnum.Accept.getState());

            LabMember recommendMember = labMemberService.selectById(audit.getRecommenderId());
            Assert.notNull(recommendMember, "实验室成员不存在");
            UserInfoVo recommendedUser = new UserInfoVo();
            recommendedUser.setId(recommendMember.getUserId());
            recommendedUser.setLabMemberId(recommendMember.getId());
            recommendedUser.setLabId(userVo.getLabId());
            recommendedUser.setLabName(userVo.getLabName());
            Long methodId = methodRecommendLogic.recommendDeal(recommendDeal, recommendedUser);
            Assert.notNull(methodId, "审核通过失败");

            Method recommendMethod = methodService.getById(audit.getMethodId());

            //修改后审核通过，执行消息推送
            RecommendMethodModifyAuditPassTemp recommendMethodModifyAuditPassTemp = new RecommendMethodModifyAuditPassTemp(audit.getLabId(), recommendMember.getUserId(), audit.getRecommenderId(), recommendMethod.getName(),methodId);
            messageProducer.sendMessagePush(recommendMethodModifyAuditPassTemp);

            //推送给实验室所有成员
            sendMsgLabMemberList(userVo, audit, recommendMethod);
        }

    }

    public void notAdopt(Long labId, Long auditorId, Long id, String auditOpinion) {
        methodRecommendAuditService.notAdopt(labId, auditorId, id, auditOpinion);
    }

    public MethodAuditStateVo adoptMessage(Long id) {
        MethodAuditInfoVo auditInfo = methodRecommendAuditService.auditInfo(id);
        Assert.notNull(auditInfo, "推荐方法审核记录不存在");
        MethodAuditStateVo vo = new MethodAuditStateVo();
        vo.setId(auditInfo.getId());
        vo.setState(1);
        if (RecommendAuditStateEnum.AuditRefuse.getState().equals(auditInfo.getState())) {
            vo.setState(3);
            String formatStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(auditInfo.getAuditTime());
            vo.setMessage("当前方法已经被" + auditInfo.getAuditorName() + "于" + formatStr + "拒绝。");
        } else if (RecommendAuditStateEnum.Recommend.getState().equals(auditInfo.getState())
                || RecommendAuditStateEnum.RecommendCancel.getState().equals(auditInfo.getState())) {
            vo.setState(2);
            String formatStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(auditInfo.getAuditTime());
            vo.setMessage("当前方法已经被" + auditInfo.getAuditorName() + "于" + formatStr + "审核通过。");
        }
        return vo;
    }

    /**
     * @return java.util.List<com.brillilab.domain.vo.method.MethodListItem>
     * @Description: 获取审核人存在的相同方法列表
     * @Params: [otherMethodId, user]
     * @author wuzhiyu
     * @date 2019/3/6 13:20
     */
    public List<MethodListItem> sameMethodList(Long methodId, UserInfoVo user) {
        Method method = methodService.getById(methodId);
        Assert.notNull(method, "数据不存在");
        List<MethodListItem> result = new ArrayList<>();
        List<Method> recommendList = methodService.listRecommendMethod(user.getLabId());
        List<MethodType> recommendThirdMethodTypeList = methodTypeService.listRecommendMethodType(user.getLabId(),
                MethodTypeLevelEnum.LevelThird);
        List<MethodType> recommendSecondMethodTypeList = methodTypeService.listRecommendMethodType(user.getLabId(),
                MethodTypeLevelEnum.LevelSecond);
        List<MethodType> recommendFirstMethodTypeList = methodTypeService.listRecommendMethodType(user.getLabId(),
                MethodTypeLevelEnum.LevelFirst);
        Map<Long, Map<Integer, MethodType>> recommendTypeCache = recommendThirdMethodTypeList.stream()
                .collect(Collectors.toMap(MethodType::getId, t -> {
                    Map<Integer, MethodType> map = new HashMap<>();
                    MethodType first = recommendFirstMethodTypeList.stream()
                            .filter(f -> f.getId().equals(t.getFirstId())).findFirst().get();
                    MethodType sec = recommendSecondMethodTypeList.stream()
                            .filter(f -> f.getId().equals(t.getSecondId())).findFirst().get();
                    map.put(1, first);
                    map.put(2, sec);
                    return map;
                }));
        List<MethodType> methodThreeTypeList = methodTypeService.listThreeLevelTypeByThirdId(method.getMethodTypeId());
        MethodType methodThirdType = methodThreeTypeList.stream()
                .filter(t -> t.getLevel().equals(MethodTypeLevelEnum.LevelThird.getLevel())).findFirst().get();
        String methodSecType = methodThreeTypeList.stream()
                .filter(o -> o.getLevel().equals(MethodTypeLevelEnum.LevelSecond.getLevel())).findFirst().get()
                .getName();
        String methodFirstType = methodThreeTypeList.stream()
                .filter(o -> o.getLevel().equals(MethodTypeLevelEnum.LevelFirst.getLevel())).findFirst().get()
                .getName();
        Set<Long> methodIdCache = new HashSet<>();
        List<MethodType> sameNameRecommendThirdTypeList = recommendThirdMethodTypeList.stream()
                .filter(t -> t.getName().equals(methodThirdType.getName()))
                .collect(Collectors.toList());

        if (method.getSourceMethodId() != null || !CollectionUtils.isEmpty(sameNameRecommendThirdTypeList)) {
            recommendList.forEach(r -> {
                //推荐列表同源
                if (method.getSourceMethodId() != null) {
                    List<Method> sameSourceList = recommendList.stream()
                            .filter(m -> method.getSourceMethodId().equals(m.getSourceMethodId()))
                            .collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(sameSourceList)) {
                        sameSourceList.forEach(m -> {
                            if (!methodIdCache.contains(m.getId()) && !m.getId().equals(method.getId())) {
                                addMethodListItem(result, methodIdCache, m, user.getLabId());
                            }
                        });
                    }
                }
                if (!CollectionUtils.isEmpty(sameNameRecommendThirdTypeList)) {
                    if (sameNameRecommendThirdTypeList.stream().anyMatch(st -> st.getId().equals(r.getMethodTypeId()))) {
                        Map<Integer, MethodType> map = recommendTypeCache.get(r.getMethodTypeId());
                        if (!CollectionUtils.isEmpty(map)) {
                            MethodType first = map.get(1);
                            MethodType second = map.get(2);
                            if (first.getName().equals(methodFirstType) && second.getName().equals(methodSecType)) {
                                if (!methodIdCache.contains(r.getId()) && !r.getId().equals(method.getId())) {
                                    addMethodListItem(result, methodIdCache, r, user.getLabId());
                                }
                            }
                        }
                    }
                }
            });
        }
        if (method.getSourceMethodId() != null) {
            List<Method> mySameSourceMethodList = methodService.listSameSourceMethod(method.getSourceMethodId(),
                    user.getId());
            if (!CollectionUtils.isEmpty(mySameSourceMethodList)) {
                mySameSourceMethodList.forEach(m -> {
                    if (!(methodIdCache.contains(m.getId())) && m.getVersion().equals(0) && !m.getId().equals(method.getId())) {
                        addMethodListItem(result, methodIdCache, m, user.getLabId());
                    }
                });
            }
        }
        List<MethodType> mySameNameTypeList = methodTypeService.listByNameAndUserId(methodThirdType.getName(),
                user.getId());
        if (!CollectionUtils.isEmpty(mySameNameTypeList)) {
            List<Long> sameTypeIdList = new ArrayList<>();
            List<MethodType> myAllTypeList = methodTypeService.listUserAllType(user.getId());
            List<MethodType> myThirdTypeList = myAllTypeList.stream().filter(a -> a.getLevel().equals(MethodTypeLevelEnum.LevelThird.getLevel())).collect(Collectors.toList());
            myThirdTypeList.forEach(t -> {
                MethodType first = myAllTypeList.stream()
                        .filter(at -> at.getLevel().equals(MethodTypeLevelEnum.LevelFirst.getLevel())
                                && at.getId().equals(t.getFirstId()))
                        .findFirst().get();
                MethodType second = myAllTypeList.stream()
                        .filter(at -> at.getLevel().equals(MethodTypeLevelEnum.LevelSecond.getLevel())
                                && at.getId().equals(t.getSecondId()))
                        .findFirst().get();
                if (first.getName().equals(methodFirstType) && second.getName().equals(methodSecType) && t.getName().equals(methodThirdType.getName())) {
                    sameTypeIdList.add(t.getId());
                }
            });
            if (!CollectionUtils.isEmpty(sameTypeIdList)) {
                List<Method> mySameTypeMethodList = methodService.listByTypeIdListAndCurrent(sameTypeIdList,
                        user.getId());
                mySameTypeMethodList.forEach(m -> {
                    if (!methodIdCache.contains(m.getId()) && !m.getId().equals(method.getId())) {
                        addMethodListItem(result, methodIdCache, m, user.getLabId());
                    }
                });
            }
        }
        return result;
    }

    public void addMethodListItem(List<MethodListItem> result, Set<Long> methodIdCache, Method m, Long labId) {
        methodIdCache.add(m.getId());
        MethodListItem item = new MethodListItem();
        item.setSuccessCount(m.getSuccessCount());
        item.setId(m.getId());
        item.setName(m.getName());
        item.setExportCount(m.getExportCount());
        item.setSourceName(MethodSourceEnum.getName(m.getFromType()));
        item.setSourceMethodId(m.getSourceMethodId());
        MethodRecommendAudit audit = methodRecommendAuditService.getRecommendAuditByMethodId(m.getId(), labId);
        item.setIsRecommend(m.getRecommendMethodId() == null ? false : audit == null ? false : audit.getState().equals(RecommendAuditStateEnum.Recommend.getState()) ? true : false);
        result.add(item);
    }

    /**
     * 管理员收到成员推荐方法(待审核)
     *
     * @param userVo
     * @return
     */
    public List<HomeMyAuditVo> getAllAuditList(UserInfoVo userVo) {
        List<HomeMyAuditVo> auditList = new ArrayList<>();

        //检测当前用户是否拥有方法管理权限
        if (userVo.getPowers() != null && userVo.getPowers().size() > 0) {
            LabMemberPower labMemberPower = userVo.getPowers().stream().filter(p -> p.getPower().equals(LabMemberPowerEnum.Power.METHOD.getValue())).findAny().orElse(null);
            if (labMemberPower == null) {
                return auditList;
            }
        }

        //管理员收到成员推荐方法(待审核)
        List<MethodRecommendAudit> methodRecommendAuditList = methodRecommendAuditService.getByLabIdAndState(userVo.getLabId(), 0);
        if (methodRecommendAuditList != null && methodRecommendAuditList.size() > 0) {
            HomeMyAuditVo audit = null;
            for (MethodRecommendAudit recommendAudit : methodRecommendAuditList) {
                //管理员收到成员推荐方法
                audit = new HomeMyAuditVo();
                audit.setType(MessageTypeEnum.ReceiveMemberRecommendMethod.getValue());
                audit.setSendTime(recommendAudit.getCreateTime());
                audit.setReferId(recommendAudit.getId());//业务关联Id为审核id
                auditList.add(audit);
            }
        }
        return auditList;
    }

    /**
     * 管理员收到成员推荐方法(待审核)
     *
     * @param userVo
     * @return
     */
    public List<HomeMyAuditVo> getAuditList(List<HomeMyAuditVo> auditVoList, UserInfoVo userVo) {
        List<HomeMyAuditVo> auditList = new ArrayList<>();
        if (CollectionUtils.isEmpty(auditVoList) && auditVoList.size() > 0) {
            return auditList;
        }

        List<Long> auditIdList = auditVoList.stream().map(p -> p.getReferId()).collect(Collectors.toList());
        //待审核推荐方法
        List<MethodRecommendAudit> methodRecommendAuditList = methodRecommendAuditService.selectByIds(auditIdList);
        //方法Id集合
        List<Long> methodIdList = methodRecommendAuditList.stream().map(p -> p.getMethodId()).collect(Collectors.toList());
        //方法集合
        List<Method> methodList = methodService.selectListByIds(methodIdList);
        //推荐人LabMemberId集合
        List<Long> labMemberIdList = methodRecommendAuditList.stream().map(p -> p.getRecommenderId()).collect(Collectors.toList());
        //推荐人成员集合
        List<LabMember> labMemberList = labMemberService.selectList(labMemberIdList);
        //推荐人用户Id集合
        List<Long> userIdList = labMemberList.stream().map(p -> p.getUserId()).collect(Collectors.toList());
        //推荐人用户信息集合
        List<Users> userList = usersLogic.getUserList(userIdList);
        for (HomeMyAuditVo auditVo : auditVoList) {
            MethodRecommendAudit recommendAudit = methodRecommendAuditList.stream().filter(p -> p.getId().equals(auditVo.getReferId())).findAny().orElse(null);
            if (recommendAudit == null) {
                continue;
            }
            Method recommendMethod = methodList.stream().filter(p -> p.getId().equals(recommendAudit.getMethodId())).findAny().orElse(null);
            LabMember labMember = labMemberList.stream().filter(p -> p.getId().equals(recommendAudit.getRecommenderId())).findAny().orElse(null);
            if (labMember == null) {
                continue;
            }
            Users executorUser = userList.stream().filter(p -> p.getId().equals(labMember.getUserId())).findAny().orElse(null);
            //成员姓名
            String memberName = executorUser != null ? executorUser.getName() : "";
            //方法名
            String methodName = recommendMethod != null ? recommendMethod.getName() : "";
            String content = String.format("<span>您收到</span><span style='color:#F6AC2D'>%s</span><span>推荐的</span>" +
                            "<span style='color:#F6AC2D'>%s</span><span>方法</span>",
                    memberName, methodName);
            auditVo.setContent(content);
            auditList.add(auditVo);
        }
        return auditList;
    }
}
